package com.hurl.waguji.base.dao;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hurl.library.jdbc.DBConfig;
import com.hurl.library.jdbc.DBManager;

public class HibernateDao {
	private static Logger log = Logger.getLogger(HibernateDao.class);
	
	private Session s;
	private Transaction tx;
	
	private DBManager jdbc;
	
	public DBManager getJDBC(){
		if(jdbc==null){
			DBConfig config = new DBConfig();
			config.setConnectUrl("jdbc:mysql://localhost:3306/twooneo1_waguji?useUnicode=true&amp;characterEncoding=UTF-8");
			config.setDriverClass("com.mysql.jdbc.Driver");
			config.setUser("twooneo1_waguji");
			config.setPassword("waguji");
			jdbc = DBManager.newInstance(config);
		}
		return this.jdbc;
	}
	
	public HibernateDao(Session s){
		this.s = s;
		this.beginTransaction();
	}
	
	/**
	 * 查询一个BO对象，不支持延迟加载，不查询二级缓存
	 * @param <T>
	 * @param clazz
	 * @param id
	 * @return 找不到返回NULL
	 */
	public <T/* extends PersistentBO*/> T get(Class<T> clazz, Serializable id){
		return (T)s.get(clazz, id);
	}
	
	/**
	 * 查询一个BO对象，支持延迟加载，支持二级缓存。
	 * @param <T>
	 * @param clazz
	 * @param id
	 * @return 找不到则报错
	 */
	public <T/* extends PersistentBO*/> T load(Class<T> clazz, Serializable id){
		return (T)s.load(clazz, id);
	}
	
	public <T/* extends PersistentBO*/> Criteria createCriteria(Class<T> clazz){
		return s.createCriteria(clazz);
	}
	
	public void save(Object/*PersistentBO*/ bo){
		log.trace("BO: "+bo);
		s.save(bo);
	}
	public void saveList(List<?> bolist){
		if(bolist!=null && !bolist.isEmpty()){
			for(Object bo : bolist){
				log.debug("save BO: "+bo);
				s.save(bo);
			}
		}
	}
	public void saveOrUpdateList(List<?> bolist){
		if(bolist!=null && !bolist.isEmpty()){
			for(Object bo : bolist){
				log.debug("save BO: "+bo);
				s.saveOrUpdate(bo);
			}
		}
	}
	public void update(Object/*PersistentBO*/ bo){
		s.update(bo);
	}
	public void saveOrUpdate(Object/*PersistentBO*/ bo){
		s.saveOrUpdate(bo);
	}
	public boolean contains(Object object){
		return s.contains(object);
	}
	public void delete(Object bo){
		s.delete(bo);
	}
	
	/**
	 * 使用原生SQL语句查询
	 * @param sql
	 * @return
	 */
	public SQLQuery queryBySQL(String sql){
		return s.createSQLQuery(sql);
	}
	
	/**
	 * 使用原生SQL语句查询，支持参数
	 * @param sql
	 * @param params
	 * @return
	 */
	/*public SQLQuery queryBySQL(String sql,Object[] params){
		SQLQuery q = s.createSQLQuery(sql);
		q = (SQLQuery)addQueryParameters(q,params);
		return q;
	}*/
	
	/**
	 * 使用HQL语句查询
	 * @param hql
	 * @return
	 */
	public Query queryByHQL(String hql){
		return s.createQuery(hql);
	}

	private Query addQueryParameters(Query q, Object[] params){
		if(params!=null && params.length>0){
			for(int i=0; i<params.length; i++){
				q = q.setParameter(i, params[i]);
			}
		}
		return q;
	}
	private HibernateDao beginTransaction(){
		tx = s.beginTransaction();
		return this;
	}
	
	public void rollback(){
		if(tx!=null){
			tx.rollback();
		}
	}
	
	public void commit(){
		if(tx!=null){
			tx.commit();
		}
	}
	
	public void clear(){
		s.clear();
	}
	
	public void flush(){
		s.flush();
	}
	
	public void evict(Object/*PersistentBO*/ bo){
		s.evict(bo);
	}
	
	public void showStatus(){
		System.out.println("session is closed: "+!s.isOpen());
		System.out.println("session is connected: "+!s.isConnected());
		s.flush();
		s.close();
		s.disconnect();
	}
	/*public void close(){
		if(s.isOpen()){
			s.close();
		}
	}*/
	
	/** JDBC Connection */
}
